<template>
    <div class="loginbBody">
        <div class="loginDiv">
            <div class="login-content">
                <h1 class="login-title">用户登录</h1>
                <el-form :model="loginForm" label-width="100px"
                         :rules="rules" ref="loginForm">
                    <el-form-item label="名字" prop="name">
                        <el-input style="width: 200px" type="text" v-model="loginForm.name"
                                  autocomplete="off" size="small"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="password">
                        <el-input style="width: 200px" type="password" v-model="loginForm.password"
                                  show-password autocomplete="off" size="small"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="confirm">登 录</el-button>
                        <el-button type="primary" @click="registered">注 册</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>

    import { ElMessageBox } from 'element-plus'
    import axios from 'axios'

    export default {
        name: 'Login',
        data(){
            return{
                loginForm:{
                    name:'',
                    password:''
                },
                rules:{
                    name: [
                        { required: true, message: '请输入用户名', trigger: 'blur' },
                        { min: 3, max: 6, message: '用户名长度在 3 到 6 个字符', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输密码', trigger: 'blur' },
                        { min: 3, max: 6, message: '密码长度在 3 到 6 个字符', trigger: 'blur' }
                    ]
                }
            }
        },
        methods:{
            confirm(){
                this.$refs.loginForm.validate((valid) => {
                    //valid成功为true，失败为false
                    if (valid) {
                        let formData = new FormData();
                        formData.append('name', this.loginForm.name);
                        formData.append('password', this.loginForm.password);
                        axios.post('http://localhost:8081/login', formData, {
                            headers: {
                                'Content-Type': 'application/json;charset=UTF-8',//设置请求头请求格式为JSON
                            },
                        }).then(
                            (res) => {
                                console.log("用户数据", res.data);
                                if (res.data.code == '200') {
                                    setTimeout(() => {
                                        ElMessageBox.alert('用户注册成功', res.data.msg, {
                                            confirmButtonText: 'OK',
                                        })
                                        this.$router.push({path:'/UserInfo'})
                                    }, 500);
                                } else {
                                    setTimeout(() => {
                                        ElMessageBox.alert('用户注册失败', res.data.msg, {
                                            confirmButtonText: 'OK',
                                        })
                                    }, 500);
                                }
                            }
                        )
                    } else {
                        console.log('校验失败');
                        return false;
                    }
                });
            },
            registered(){
                this.$router.replace('/Register');
            }
        }
    }
</script>

<style scoped >
    .loginbBody {
        width: 100%;
        height: 100%;
        background-color: #B3C0D1;
    }
    .loginDiv {
        position: absolute;
        top: 50%;
        left: 50%;
        margin-top: -200px;
        margin-left: -250px;
        width: 450px;
        height: 330px;
        background: #fff;
        border-radius: 5%;

    }
    .login-title {
        margin: 20px 0;
        text-align: center;
    }
    .login-content {
        width: 400px;
        height: 250px;
        position: absolute;
        top: 25px;
        left: 25px;
    }
</style>